package com.dwb.controller;

import com.dwb.dto.SGoodsDto;
import com.dwb.entity.SGoods;
import com.dwb.result.Result;
import com.dwb.service.SGoodsService;
import com.dwb.utils.Empty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import static com.dwb.common.check.Check.CHECK_GOODS_ADD_UPDATE;

@Api(value = "SGoods",description = "服务产品控制器")
@RestController
@RequestMapping("/SGoods")
public class SGoodsController {
    @Autowired
    private SGoodsService sGoodsService;


    /**
     * 更改是否是首页推荐商品
     * @param parameters 传入id 和 isHomeRecommended字段value
     * @return
     */
    @ApiOperation(value = "updateRecommend",notes = "是否是首页推荐商品")
    @PostMapping("/updateRecommend")
    public Result<String> updateRecommend(@RequestBody Map<String ,String> parameters){
        sGoodsService.updateRecommend(parameters);
        return Result.newSuccess();
    }


    /**
     * 更改商品状态
     * @return
     */
    @ApiOperation(value = "updateState",notes = "更改商品状态")
    @PostMapping("/updateState")
    public Result<String> updateState(@RequestBody Map<String ,String> parameters){
        sGoodsService.updateState(parameters);
        return Result.newSuccess();
    }

    /**
     * 删除商品
     * @param parameters
     * @return
     */
    @ApiOperation(value = "delete",notes = "删除商品")
    @PostMapping("/delete")
    public Result<String > delete(@RequestBody Map<String ,String > parameters){
        sGoodsService.delete(parameters);


        return Result.newSuccess();

    }



    /**
     * 获取产品列表
     * @param parameters
     * @return
     */
    @ApiOperation(value = "getList",notes = "获取产品列表")
    @PostMapping("/getList")
    public Result<PageInfo<SGoodsDto>> getList(@RequestBody Map<String ,String > parameters){
        Result<PageInfo<SGoodsDto>> result = Result.newSuccess();
        PageInfo<SGoodsDto> list = sGoodsService.getList(parameters);
        result.setDataMap(list);
        return result;

    }



    /**
     * 获取服务产品详情信息
     * @param parameters
     * @return
     */
    @ApiOperation(value = "getInfo",notes = "获取服务产品详情信息")
    @PostMapping("/getInfo")
    public Result<Map<String ,Object>> getInfo(@RequestBody Map<String,String> parameters){
        Result<Map<String ,Object>> result = Result.newSuccess();
        Map<String,Object> map = sGoodsService.getInfo(parameters);
        result.setDataMap(map);
        return result;
    }

    /**
     * 添加或者更新服务产品信息
     * @param parameters
     * @return
     */
    @ApiOperation(value = "addOrUpdate",notes = "添加or修改商品")
    @PostMapping("/addOrUpdate")
    public Result<String> addOrUpdate(@RequestBody Map<String,Object> parameters){
        CHECK_GOODS_ADD_UPDATE(parameters); //校验
        Object sGoods = parameters.get("sGoods");
        ObjectMapper objectMapper = new ObjectMapper();
        SGoods sGoodsV = objectMapper.convertValue(sGoods, SGoods.class);
        if (Empty.INT_EMPTY(sGoodsV.getId())){
            // 添加
            sGoodsService.insert(parameters);

        }else {
            //修改
            sGoodsService.update(parameters);
        }
        return Result.newSuccess();


    }
}
